import { Product } from "../../models/product"

export const GET_PRODUCT = "GET_PRODUCT"
export const GET_PRODUCT_SUCCESS = "GET_PRODUCT_SUCCESS"
export const SEARCH_PRODUCT = "SEARCH_PRODUCT"
export const SEARCH_PRODUCT_SUCCESS = "SEARCH_PRODUCT_SUCCESS"
export const FILTER_PRODUCT = "FILTER_PRODUCT"
export const FILTER_PRODUCT_SUCCESS = "FILTER_PRODUCT_SUCCESS"
export const GET_PRODUCTBY_ID = "GET_PRODUCTBY_ID"
export const GET_PRODUCTBY_ID_SUCCESS = "GET_PRODUCTBY_ID_SUCCESS"

export interface getProductAction {
  type: typeof GET_PRODUCT,
  sortBy: string,
  order: string,
  limit: number
}

export interface getProductSuccessAction {
  type: typeof GET_PRODUCT_SUCCESS,
  payload: Product[],
  sortBy: string
}

export interface searchProductAction {
  type: typeof SEARCH_PRODUCT,
  payload: {
    category: string,
    search: string
  }
}

export interface searchProductSuccessAction {
  type: typeof SEARCH_PRODUCT_SUCCESS,
  products: Product[]
}

export interface filterPayload {
  order?: string,
  limit?: number,
  sortBy?: string,
  skip: number,
  filters?: {
    category: string[],
    price: number[]
  }
}

export interface filterProductAction {
  type: typeof FILTER_PRODUCT,
  payload: filterPayload
}

export interface filterProductSuccessAction {
  type: typeof FILTER_PRODUCT_SUCCESS,
  payload: {
    size: number,
    data: Product[]
  },
  skip: number
}

export interface getProductByIdAction {
  type: typeof GET_PRODUCTBY_ID,
  payload: {
    productId: string
  }
}
export interface getProductByIdSuccessAction {
  type: typeof GET_PRODUCTBY_ID_SUCCESS,
  payload: Product
}

export const getProduct = (sortBy = "createdAt", order = "desc", limit = 10): getProductAction => ({
  type: GET_PRODUCT,
  sortBy,
  order,
  limit
})

export const getProductSuccess = (payload: Product[], sortBy: string): getProductSuccessAction => ({
  type: GET_PRODUCT_SUCCESS,
  payload,
  sortBy
})

export const searchProduct = (payload: { category: string, search: string }): searchProductAction => ({
  type: SEARCH_PRODUCT,
  payload
})

export const searchProductSuccess = (products: Product[]): searchProductSuccessAction => ({
  type: SEARCH_PRODUCT_SUCCESS,
  products
})

export const filterProduct = ({limit=4, skip=0, sortBy, filters}:filterPayload):filterProductAction => ({
  type: FILTER_PRODUCT,
  payload:{
    limit,
    skip,
    sortBy, 
    filters
  }
})

export const filterProductSuccess = (payload:{size:number, data: Product[]}, skip:number):filterProductSuccessAction => ({
  type: FILTER_PRODUCT_SUCCESS,
  payload,
  skip
})

export const getProductId = (payload:{productId: string}):getProductByIdAction => ({
  type: GET_PRODUCTBY_ID,
  payload
})

export const getProductIdSuccess = (payload:Product):getProductByIdSuccessAction => ({
  type: GET_PRODUCTBY_ID_SUCCESS,
  payload
})

export type productUnionType = getProductAction | 
                              getProductSuccessAction | 
                              searchProductAction | 
                              searchProductSuccessAction |
                              filterProductAction |
                              filterProductSuccessAction|
                              getProductByIdAction |
                              getProductByIdSuccessAction